<template>
	<div :class="['widgets-home', customizing ? 'customizing' : '']" ref="main">
		<div class="widgets-content">
			<el-form :model="form" :rules="rules" ref="dialogForm" :disabled="mode == 'show'" label-width="110px" label-position="top">
				<el-row :gutter="1">
					<el-col :span="24">
						<div class="titleBox">
                            {{mode=='show'?'查看供应商':mode == 'edit'?'编辑供应商':'新增供应商'}}
                            <div>
                                <!--<el-button @click="resetForm('dialogForm')" size="">退出</el-button>-->
                                <el-button type="primary" @click="submit" :disabled="mode == 'show'">保存</el-button>
                                <!-- <el-button type="primary" @click="submit" :disabled="mode == 'show'">保存并新增</el-button>
                                <el-button type="danger" @click="back" :disabled="mode == 'show'">退出</el-button> -->
                            </div>
						</div>
					</el-col>
				</el-row>
				<el-card class="box-card">
					<el-row :gutter="1">
						<el-col :span="24">
							<div class="titleBox titleBox1">
								基本信息
							</div>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="3">
							<el-form-item label="编码" prop="code">
								<el-input v-model="form.code" placeholder="请输入(不填写自动生成)" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="名称" prop="name">
								<el-input v-model="form.name" placeholder="请输入" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="分类" prop="cate_id">
                                <el-select v-model="form.cate_id" placeholder="请选择分类" style="width:100%" clearable filterable>
                                    <el-option v-for="item in cateList" :key="item.id" :label="item.cate_name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="联系人" prop="linkman">
								<el-input v-model="form.linkman" placeholder="请输入" clearable></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="手机号" prop="mobile">
								<el-input v-model="form.mobile" placeholder="请输入" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="3">
							<el-form-item label="省份" prop="province">
                                <el-select v-model="form.province" placeholder="请选择省份" style="width:100%" clearable filterable @change="getArea(form.province)">
                                    <el-option v-for="item in provinceList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="市区" prop="city">
                                <el-select v-model="form.city" placeholder="请选择市区" style="width:100%" clearable filterable @change="getCounty(form.city)">
                                    <el-option v-for="item in areaList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="3">
							<el-form-item label="区/县" prop="area">
                                <el-select v-model="form.area" placeholder="请选择区/县" style="width:100%" clearable filterable>
                                    <el-option v-for="item in countyList" :key="item.id" :label="item.name" :value="item.id"/>
                                </el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :sm="9">
							<el-form-item label="具体地址" prop="address">
								<el-input v-model="form.address" placeholder="具体地址" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-card>
				<el-card class="box-card">
					<el-row :gutter="1">
						<el-col :span="24">
							<div class="titleBox titleBox1">
								其他信息
							</div>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="15">
							<el-form-item label="备注" prop="desc">
								<el-input type="textarea" v-model="form.desc" placeholder="请输入" clearable></el-input>
							</el-form-item>
						</el-col>
					</el-row>
					<el-row :gutter="1">
						<el-col :xs="24" :sm="15">
							<el-form-item label="附件" prop="apply_files">
								<sc-upload-file v-model="form.apply_files" :limit="1" :data="{otherData:'demo'}" tip="" >
									<el-button type="primary" icon="el-icon-upload">选择文件</el-button>
								</sc-upload-file>
							</el-form-item>
						</el-col>
					</el-row>
				</el-card>
			</el-form>
		</div>
	</div>
</template>

<script>

export default {
	name: '新增BOM',
	components: {},
	data() {
		return {
			dialog: {

			},
			//表单数据
			form: {
				id: "",
				name: "",
				code: "",
				cate_id: "",
				linkman: "",
				mobile: "",
				address: "",
				desc: "",
				apply_files: "",
                province: "",
                area: "",
                county: "",
			},
			//验证规则
			rules: {
				goods_name: [{ required: true, message: '请输入物料名称' }],
			},
			cateList:[],
            provinceList:[],
            areaList:[],
            countyList:[],
            mode:'',
		}
	},
	mounted() {
		this.getCate()
		this.getProvince()
        if (this.$route.query.id) {
			this.mode = this.$route.query.zhuangtai
			this.setData()
		}
	},
	methods: {
        // 获取分类
		async getCate(){
			var res = await this.$API.basicdata.applyCate.list.get();
			this.cateList = res.data
		},
        // 省
        async getProvince(){
			var res = await this.$API.all.getAddress.get();
			this.provinceList = res.data
		},
        // 省切换获取市
        async getArea(e){
            this.form.city = ''
            this.form.area = ''
            this.areaList = []
            this.countyList = []
            if (!e) {
                return
            }
			var res = await this.$API.all.getAddress.get({parent_id:e});
			this.areaList = res.data
		},
        // 省切换获取区/县
        async getCounty(e){
            this.form.area = ''
			var res = await this.$API.all.getAddress.get({parent_id:e});
			this.countyList = res.data
		},
		//表单注入数据
		async setData() {
			var reqData = {id: this.$route.query.id}
			var res = await this.$API.basicdata.applyList.detail.post(reqData);
			this.form.id = this.$route.query.id
			this.form.name = res.data.name
			this.form.code = res.data.code
			this.form.cate_id = res.data.cate_id
			this.form.linkman = res.data.linkman
			this.form.mobile = res.data.mobile
			this.form.desc = res.data.desc
			this.form.apply_files = res.data.apply_files
            this.form.province = res.data.province
            this.getArea(res.data.province)
            this.form.city = res.data.city
            this.getCounty(res.data.city)
            this.form.area = res.data.area
			this.form.address = res.data.address
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.basicdata.applyList.edit.post(this.form);
					} else {
						res = await this.$API.basicdata.applyList.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						// this.$emit('success', this.form, this.mode)
						// this.visible = false;
						this.$message.success("操作成功")
                        if (this.mode == '') {
                            this.resetForm('dialogForm')
						} else {
							this.setData()
						}
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
        resetForm(formName) {
			this.$refs[formName].resetFields();
		},
	}
}
</script>
<style>
.sc-upload-multiple[data-v-f680f9e4] .el-upload--picture-card {
	width: 60px;
	height: 60px;
}

[data-v-f680f9e4] .el-upload-list__item {
	width: 60px;
	height: 60px;
}
</style>
<style lang="scss">
.widgets-home {
	display: flex;
	flex-direction: row;
	flex: 1;
	min-height: 100%;
	max-height: 100%;
}

.widgets-content {
	flex: 1;
	overflow: auto;
	overflow-x: hidden;
	padding: 15px 15px;
	.selectBox {
		display: flex;
		align-items: center;
	}
	.el-col {
		margin-right: 20px;
	}
	.el-form-item {
		flex-direction: column;
		justify-content: flex-start;
		align-items: flex-start;
		.el-form-item__label {
			justify-content: flex-start;
		}
		.el-form-item__content {
			width: 100%;
		}
	}
	.box-card {
		margin-bottom: 10px;
	}
	.box-card1 {
		.el-form-item__label {
			justify-content: flex-start;
			height: 0;
		}
	}
	.box-card2 {
		.el-col {
			margin-right: 0;
		}
	}
	.el-form {
		//background: #ffffff;
		//padding: 20px;
		min-height: 90%;
		.titleBox {
			font-size: 18px;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			color: #333333;
			padding-bottom: 10px;
			border-bottom: 1px solid #E6E6E6;
			margin-bottom: 14px;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}
		.titleBox1 {
			font-weight: 600;
			border: none;
			font-size: 16px;
		}
	}
}

.customizing .widgets-wrapper {
	margin-right: -360px
}

.customizing .widgets-wrapper .el-col {
	padding-bottom: 15px;
}

.customizing .widgets-wrapper .draggable-box {
	border: 1px dashed var(--el-color-primary);
	padding: 15px;
}

.customizing .widgets-wrapper .no-widgets {
	display: none;
}

.customizing .widgets-item {
	position: relative;
	margin-bottom: 15px;
}

@media (max-width: 992px) {
	.customizing .widgets {
		transform: scale(1) !important;
	}

	.customizing .widgets-aside {
		width: 100%;
		position: absolute;
		top: 50%;
		right: 0;
		bottom: 0;
	}

	.customizing .widgets-wrapper {
		margin-right: 0;
	}
}
</style>
